package com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.entity.HeaManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.repository.HeaManageRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.personnelManagement.entity.PersonnelManagement;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class HeaManageRepositoryImpl implements HeaManageRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String xsemId) {
        StringBuilder sql = new StringBuilder("select * from xczh_headquarters_management");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        int firstIndex = (curPage - 1) * pageSize;
        int lastIndex = pageSize;
        StringBuilder limitSql = new StringBuilder(" limit "+lastIndex+" offset "+ firstIndex);
        if (!PlatformObjectUtils.isEmpty(xsemId)){
            whereSql.append(" and xsem_id = '" + xsemId+ "'");
        }
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(HeaManage.class)).size();
        List<HeaManage> list = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(HeaManage.class));
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", list);
        return map;
    }

    @Override
    public HeaManage findById(String id) {
        String sql="select * from xczh_headquarters_management where xhm_id = ? ";
        HeaManage heaManage = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(HeaManage.class), id);
        return heaManage;
    }

    @Override
    public void delete(String id) {
        String sql = "update xczh_headquarters_management set del_flag= ? where xhm_id = ?";
        jdbcTemplate.update(sql, YNEnum.Y.toString(),id);
    }

    @Override
    public void save(HeaManage heaManage) {
        String sql="insert into xczh_headquarters_management(xhm_id,xsem_id,head_code,head_name,leader,tel," +
                "create_user_name,create_user,create_time,update_user,update_time,del_flag,create_unit,comment_info,leader_id) " +
                "values(:xhmId, :xsemId, :headCode, :headName, :leader, :tel, :createUserName," +
                ":createUser, :createTime,:updateUser, :updateTime, :delFlag, :createUnit, :commentInfo, :leaderId)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(heaManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void update(HeaManage heaManage) {
        String sql="update xczh_headquarters_management set head_name = :headName,comment_info = :commentInfo " +
                ",leader = :leader, leader_id = :leaderId, tel = :tel, update_user = :updateUser ,update_time = :updateTime where xhm_id = :xhmId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(heaManage);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public String getMaxNum(String str) {
        String sql="select max(head_code) from xczh_headquarters_management where del_flag = '0' and head_code like '%"+str+"%'";
        String maxParentNum=jdbcTemplate.queryForObject(sql,String.class);
        return maxParentNum;
    }

    @Override
    public void updateXsemId(String xsemId, String convertedXsemId) {
        String sql = "update xczh_headquarters_management set xsem_id = '"+xsemId+"'  where xsem_id = '"+convertedXsemId+"'";
        jdbcTemplate.update(sql);
    }

}
